package top.lv666.graduation_design_serve.controller;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import top.lv666.graduation_design_serve.pojo.Blog;
import top.lv666.graduation_design_serve.service.BlogService;
import top.lv666.graduation_design_serve.service.BlogServiceImpl;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/blog")
public class BlogController {
    // log4j
    public Logger logger = Logger.getLogger(UserController.class);
    // 减少重复map的内存申请
    private final Map<String, Object> returnMap = new HashMap<>();
    private final Map<String, Object> paramMap = new HashMap<>();

    @Autowired
    private BlogService blogService;

    /**
     * 我关注的人发布的博客
     */
    @GetMapping("/getMyFocusBlog")
    public Map<String, Object> getMyFocusBlog(
            @RequestParam int getNumber,
            @RequestParam int getPage,
            HttpServletRequest request
    ) {
        returnMap.clear();
        try {
            paramMap.clear();
            paramMap.put("request", request);
            paramMap.put("getNumber", getNumber);
            paramMap.put("getPage", getPage);
            returnMap.put("data", blogService.SelectMyFouceBlog(paramMap));
            returnMap.put("msg", "查询成功");
            returnMap.put("flag", true);
        } catch (Exception | Error e) {
            returnMap.put("msg", "查询失败!" + e.getLocalizedMessage());
            returnMap.put("flag", false);
            logger.error(e);
        }
        return returnMap;
    }

    /**
     * 我点赞的博客
     */
    @GetMapping("/getMyLikeBlog")
    public Map<String, Object> getMyLikeBlog(
            @RequestParam int getNumber,
            @RequestParam int getPage,
            HttpServletRequest request
    ) {
        returnMap.clear();
        try {
            paramMap.clear();
            paramMap.put("request", request);
            paramMap.put("getNumber", getNumber);
            paramMap.put("getPage", getPage);
            returnMap.put("data", blogService.SelectMyLikeBlog(paramMap));
            returnMap.put("msg", "查询成功");
            returnMap.put("flag", true);
        } catch (Exception | Error e) {
            returnMap.put("msg", "查询失败!" + e.getLocalizedMessage());
            returnMap.put("flag", false);
            logger.error(e);
        }
        return returnMap;
    }

    /**
     * @apiNote 删除博客
     */
    @PostMapping("/deleteBlog")
    public Map<String, Object> deleteBlog(
            @RequestBody Map<String, Object> map,
            HttpServletRequest request
    ) {
        returnMap.clear();
        try {
            map.put("request", request);
            returnMap.put("msg", "删除成功");
            returnMap.put("flag", true);
            blogService.DeleteBlog(map);
        } catch (Exception | Error e) {
            returnMap.put("msg", "删除失败!" + e.getLocalizedMessage());
            returnMap.put("flag", false);
            logger.error(e);
        }
        return returnMap;
    }

    /**
     * @apiNote 查询该自己下的博客
     */
    @GetMapping("/getBlogByUser")
    public Map<String, Object> getBlogByUser(
            @RequestParam int getNumber,
            @RequestParam int getPage,
            HttpServletRequest request
    ) {
        returnMap.clear();
        paramMap.clear();
        paramMap.put("getNumber", getNumber);
        paramMap.put("getPage", getPage);
        paramMap.put("request", request);
        try {
            returnMap.put("data", blogService.SelectBlogByUser(paramMap));
            returnMap.put("msg", "查询成功！");
            returnMap.put("flag", true);
        } catch (Exception | Error e) {
            returnMap.put("msg", "查询失败！");
            returnMap.put("flag", false);
            logger.error(e);
        }
        return returnMap;
    }

    /**
     * @apiNote 查询粉丝下的博客
     */
    @GetMapping("/getBlogByUserId")
    public Map<String, Object> getBlogByUserId(
            @RequestParam int getNumber,
            @RequestParam int getPage,
            @RequestParam String Id,
            HttpServletRequest request
    ) {
        returnMap.clear();
        paramMap.clear();
        paramMap.put("getNumber", getNumber);
        paramMap.put("getPage", getPage);
        paramMap.put("id", Id);
        paramMap.put("request", request);
        try {
            returnMap.put("data", blogService.SelectBlogByUserId(paramMap));
            returnMap.put("msg", "查询成功！");
            returnMap.put("flag", true);
        } catch (Exception | Error e) {
            returnMap.put("msg", "查询失败！");
            returnMap.put("flag", false);
            logger.error(e);
        }
        return returnMap;
    }

    /**
     * @apiNote 创建博客
     */
    @PostMapping("/createBlog")
    public Map<String, Object> CreateBlog(
            @RequestBody Map<String, Object> map,
            HttpServletRequest request
    ) {
        returnMap.clear();
        try {
            map.put("request", request);
            blogService.CreateBolg(map);
            returnMap.put("msg", "发表成功");
            returnMap.put("flag", true);
        } catch (Exception | Error e) {
            returnMap.put("msg", "发表失败!" + e.getLocalizedMessage());
            returnMap.put("flag", false);
            logger.error(e);
        }
        return returnMap;
    }

    @PostMapping("/UpdateBlog")
    public Map<String, Object> UpdateBlog(
            @RequestBody Map<String, Object> map,
            HttpServletRequest request
    ) {
        returnMap.clear();
        try {
            map.put("request", request);
            blogService.UpdateBlog(map);
            returnMap.put("msg", "修改成功");
            returnMap.put("flag", true);
        } catch (Exception | Error e) {
            returnMap.put("msg", "修改失败!" + e.getLocalizedMessage());
            returnMap.put("flag", false);
            logger.error(e);
        }
        return returnMap;
    }

    @PostMapping("/getHotBlog")
    public Map<String, Object> getHotBlog(
            @RequestBody Map<String, Object> map,
            HttpServletRequest request
    ) {
        returnMap.clear();
        try {
            map.put("request", request);
            List<Blog> list = blogService.SelectBlogHot(map);
            returnMap.put("flag", true);
            returnMap.put("data", list);
            returnMap.put("msg", "查询成功");
        } catch (Exception | Error e) {
            returnMap.put("msg", "查询失败!" + e.getLocalizedMessage());
            returnMap.put("flag", false);
            logger.error(e);
        }
        return returnMap;
    }

    @PostMapping("/getNowBlog")
    public Map<String, Object> getNowBlog(
            @RequestBody Map<String, Object> map,
            HttpServletRequest request
    ) {
        returnMap.clear();
        try {
            map.put("request", request);
            List<Blog> list = blogService.SelectBlogNow(map);
            returnMap.put("flag", true);
            returnMap.put("data", list);
            returnMap.put("msg", "查询成功");
        } catch (Exception | Error e) {
            returnMap.put("msg", "查询失败!" + e.getLocalizedMessage());
            returnMap.put("flag", false);
            logger.error(e);
        }
        return returnMap;
    }

    /**
     * @apiNote 处理点赞请求
     */
    @GetMapping("/giveLike")
    public Map<String, Object> giveLike(
            @RequestParam(defaultValue = "") String userId,
            @RequestParam(defaultValue = "") String blogId,
            HttpServletRequest request
    ) {
        returnMap.clear();
        paramMap.clear();
        try {
            if (userId.equals("") || blogId.equals("")) {
                throw new Error("参数异常");
            }
            paramMap.put("request", request);
            paramMap.put("userId", userId);
            paramMap.put("blogId", blogId);
            blogService.LikeBlog(paramMap);
            returnMap.put("flag", true);
            returnMap.put("msg", "添加成功");
        } catch (Error | Exception e) {
            returnMap.put("flag", false);
            returnMap.put("msg", "添加失败");
            returnMap.put("cause", e.getLocalizedMessage());
            logger.error(e);
        }
        return returnMap;
    }

    /**
     * @apiNote 处理取消点赞请求
     */
    @GetMapping("/cancelLike")
    public Map<String, Object> cancelLikeBlog(
            @RequestParam(defaultValue = "") String userId,
            @RequestParam(defaultValue = "") String blogId,
            HttpServletRequest request
    ) {
        try {
            returnMap.clear();
            paramMap.clear();
            if (userId.equals("") || blogId.equals("")) {
                throw new Error("参数异常");
            }
            paramMap.put("request", request);
            paramMap.put("userId", userId);
            paramMap.put("blogId", blogId);
            blogService.cancelLikeBlog(paramMap);
            returnMap.put("flag", true);
            returnMap.put("msg", "添加成功");
        } catch (Exception | Error e) {
            returnMap.put("flag", false);
            returnMap.put("msg", "添加失败");
            returnMap.put("cause", e.getLocalizedMessage());
            logger.error(e);
        }

        return returnMap;
    }

    /**
     * @apiNote 处理转发请求
     */
    @PostMapping("/refBlog")
    public Map<String, Object> createRefBlog(
            @RequestBody Map<String, Object> map,
            HttpServletRequest request
    ) {
        returnMap.clear();
        try {
            paramMap.clear();
            map.put("request", request);
            blogService.createRefBlog(map);
            returnMap.put("flag", true);
            returnMap.put("msg", "转发成功");
        } catch (Error | Exception e) {
            returnMap.put("flag", false);
            returnMap.put("msg", "转发失败");
            returnMap.put("cause", e.getLocalizedMessage());
            logger.error(e);
        }
        return returnMap;
    }

    /**
     * @apiNote 处理转发请求
     */
    @PostMapping("/cancelRefBlog")
    public Map<String, Object> deleteRefBlog(
            @RequestBody Map<String, Object> map,
            HttpServletRequest request
    ) {
        returnMap.clear();
        try {
            map.put("request", request);
            blogService.DeleteRefBlog(map);
            returnMap.put("flag", true);
            returnMap.put("msg", "取消转发成功");
        } catch (Error | Exception e) {
            returnMap.put("flag", false);
            returnMap.put("msg", "取消转发失败");
            returnMap.put("cause", e.getLocalizedMessage());
            logger.error(e);
        }
        return returnMap;
    }
}
